<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JQuery easyui demo</title>

<style type="text/css">
#goods_fm {
	margin: 0;
	padding: 10px 30px;
}

.ftitle {
	font-size: 14px;
	font-weight: bold;
	padding: 5px 0;
	margin-bottom: 10px;
	border-bottom: 1px solid #ccc;
}

.fitem {
	margin-bottom: 5px;
}

.fitem label {
	display: inline-block;
	width: 80px;
}

.fitem input {
	width: 160px;
}
</style>


<link rel="stylesheet" type="text/css"
	href="jquery-easyui-1.4.1/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="jquery-easyui-1.4.1/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="jquery-easyui-1.4.1/themes/color.css">
<link rel="stylesheet" type="text/css"
	href="jquery-easyui-1.4.1/demo/demo.css">
<script type="text/javascript" src="jquery-easyui-1.4.1/jquery.min.js"></script>
<script type="text/javascript"
	src="jquery-easyui-1.4.1/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
	<!-- 
	<div style="height: 70px; width: 100%;">
		<form id="ff" method="post">
			<div style="float: left; padding: 20px 0 0 20px;">
				<label for="name">用户名:</label> <input class="easyui-validatebox"
					type="text" name="name" />
			</div>
			<div style="float: left; padding: 20px 0 0 20px;">
				<label for="email">邮箱:</label> <input class="easyui-validatebox"
					type="text" name="email" data-options="validType:'email'" />
			</div>
			<div style="float: left; padding: 20px 0 0 20px;">
				<input class="easyui-validatebox" type="button" value="查询" />
			</div>
		</form>
	</div>
	 -->

	<table id="goods_dg" class="easyui-datagrid" style="height: 470px;"
		url="goods/findAll.do" toolbar="#goods_toolbar" pagination="true"
		rownumbers="true" fitColumns="true" singleSelect="true"
		data-options="fit:false,border:false,pageSize:10,pageList:[10,15,25,30]">
		<thead>
			<tr>
				<th field="goodsNo" width="50">商品编号</th>
				<th field="goodsName" width="50">商品名称</th>
				<th field="typeNo" formatter="goodsFormat"width="50">商品类型</th>
				<th field="providerNo" formatter="providerFormat"width="50">商品供应商</th>
				<th field="specification" width="50">描述</th>
				<th field="gremark" width="50">评论</th>
			</tr>
		</thead>
	</table>
	<div id="goods_toolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="newGoods()">新增</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-edit" plain="true" onclick="editGoods()">编辑</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" plain="true" onclick="destroyGoods()">删除</a>
	</div>

	<div id="goods_dlg" class="easyui-dialog" style="padding: 10px 20px"
		closed="true" buttons="#goods_dlg-buttons">
		<div class="ftitle">详细信息</div>
		<form id="goods_fm" method="post" novalidate>
			<div class="fitem">
				<label>商品名称:</label> <input name="goodsName" class="easyui-textbox"
					required="true">
			</div>
			<div class="fitem">
				<label>商品类型	:</label> <input id=id_goodsType name="goodsType.typeNo" 
				class="easyui-combobox" url="goodstype/findList.do"
				valueField="typeNo" textField="type" required="true"
				editable="false">
			</div>
			<div class="fitem">
				<label>商品供应商	:</label> <input id="id_provider" name="provider.providerNo" 
				class="easyui-combobox" url="provider/findList.do" 
				valueField="providerNo" textField="provider" required="true" 
				editable="false">
			</div>
			<div class="fitem">
				<label>描述	:</label> <input name="specification" 
				class="easyui-textbox">
			</div>
			<div class="fitem">
				<label>说明	:</label> <input name="gremark" 
				class="easyui-textbox" >
			</div>

		</form>
	</div>
	<div id="goods_dlg-buttons">
		<a href="javascript:void(0)" class="easyui-linkbutton c6"
			iconCls="icon-ok" onclick="saveGoods()" style="width: 90px">保存</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-cancel"
			onclick="javascript:$('#goods_dlg').dialog('close')"
			style="width: 90px">取消</a>
	</div>
	<script type="text/javascript">
	
	function goodsFormat(value, row, rowindex) {
		//用参数 row 来获取 goodsType对象,再调用里面的type属性
		return row.goodsType.type;
	}

	function providerFormat(value, row, rowindex) {
		return row.provider.provider;
	}
	
	$.fn.datebox.defaults.formatter=function(date){
		var y=date.getFullYear();
		var m=date.getMonth()+1;
		var d=date.getDate();
		return y+'/'+m+'/'+d;
	}
	
	$.fn.datebox.defaults.parser=function(date){
		if(date){
			return new Date(date)
		}else{
			return new Date();
		}
	}
	
	
		var url;
		function newGoods() {
			$('#goods_dlg').dialog('open').dialog('setTitle', '新增');
			$('#goods_fm').form('clear');//清除表单数据
			url = 'goods/add.do';//新增调用的网址
			
			var providerdata = $('#id_provider').combobox('getData');
		    if(providerdata.length>0){
			  $('#id_provider').combobox('select',providerdata[0].providerNo);
		    }; 
		    
		    var goodsTypedata = $('#id_goodsType').combobox('getData');
		    if(goodsTypedata.length>0){
			  $('#id_goodsType').combobox('select',goodsTypedata[0].typeNo);
		    }; 
		}
		

		function editGoods() {
			var row = $('#goods_dg').datagrid('getSelected');
			if (row) {
				$('#goods_dlg').dialog('open').dialog('setTitle', '编辑');
				$('#goods_fm').form('clear');//清除表单数据
				$('#goods_fm').form('load', row);//重新加载编辑数据
				url = 'goods/update.do?goodsNo=' + row.goodsNo;
				
				//console.log(row.provider.providerNo);
				//从已经加载的datagride中所选择的行（row）中获取key值,修改下拉框的选中值  
				var providerdata = $('#id_provider').combobox('getData');
			    if(providerdata.length>0){
				  $('#id_provider').combobox('select',row.provider.providerNo);
			    }; 
			    
			    var goodsTypedata = $('#id_goodsType').combobox('getData');
			    if(goodsTypedata.length>0){
				  $('#id_goodsType').combobox('select',row.goodsType.typeNo);
			    }; 
			}
		}
		function saveGoods() {
			$('#goods_fm').form('submit', {
				url : url,
				onSubmit : function() {
					return $(this).form('validate');
				},
				success : function(result) {
					var result = eval('(' + result + ')');
					if (result.errorMsg) {
						$.messager.show({
							title : 'Error',
							msg : result.errorMsg
						});
					} else {
						$('#goods_dlg').dialog('close'); // close the dialog
						$('#goods_dg').datagrid('reload'); // reload the user data
					}
				}
			});
		}
		function destroyGoods() {
			var row = $('#goods_dg').datagrid('getSelected');
			if (row) {
				$.messager.confirm('Confirm', '确定要删除吗?', function(r) {
					if (r) {
						//ajax调用-异步通信调用
						$.post('goods/delete.do', {
							goodsNo : row.goodsNo
						}, function(result) {
							if (result.success) {
								$('#goods_dg').datagrid('reload'); // reload the user data
							} else {
								$.messager.show({ // show error message
									title : 'Error',
									msg : result.errorMsg
								});
							}
						}, 'json');//回调函数
					}
				});
			}
		}
	</script>

</body>
</html>